package com.andersen.database.entity;

import com.andersen.web.beans.ComponentCommandForm;
import com.andersen.web.beans.NewComponentCommandForm;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * Created with IntelliJ IDEA.
 * User: Avseenko Dmitrij
 * Date: 27.02.14
 * Time: 11:39
 * To change this template use File | Settings | File Templates.
 */
public class ComponentEntity {

    private long id;
    private long type_id;
    private ComponentTypeEntity componentType;
    private String description;
    private Date by_date;
    private Date warranty_end_date;
    private String serial;
    private boolean deleted;

    public static ComponentEntity fromCommandForm(ComponentCommandForm form) {
        ComponentEntity c = new ComponentEntity();
        SimpleDateFormat fmt = new SimpleDateFormat("dd.MM.yyyy");
        if (form.getId() != -1) c.setId(form.getId());
        try {
            if (!form.getBuyDateString().isEmpty()) 
                c.setByDate(new Date(fmt.parse(form.getBuyDateString()).getTime()));
            if (!form.getWarrantyEndDateString().isEmpty())
                c.setWarrantyEndDate(new Date(fmt.parse(form.getWarrantyEndDateString()).getTime()));
        } catch (ParseException ex) {}
        if (form.getTypeId() > 0) c.setTypeId(form.getTypeId());
        c.setDescription(form.getDescription());
        c.setDeleted(false);
        c.setSerial(form.getSerial());
        return c;
    }
    
    public static ComponentEntity fromCommandForm(NewComponentCommandForm form) {
        ComponentEntity c = new ComponentEntity();
        SimpleDateFormat fmt = new SimpleDateFormat("dd.mm.yyyy");
        try {
            if (!form.getBuyDateString().isEmpty()) 
                c.setByDate(new Date(fmt.parse(form.getBuyDateString()).getTime()));
            if (!form.getWarrantyEndDateString().isEmpty())
                c.setWarrantyEndDate(new Date(fmt.parse(form.getWarrantyEndDateString()).getTime()));
        } catch (ParseException ex) {}
        c.setTypeId(form.getTypeId());
        c.setDescription(form.getDescription());
        c.setDeleted(false);
        c.setSerial(form.getSerial());
        return c;
    }

    public long getId(){
        return id;
    }

    public long getTypeId(){
        return type_id;
    }

    public String getDescription(){
        return description;
    }

    public Date getByDate(){
        return by_date;
    }

    public Date getWarrantyEndDate(){
        return warranty_end_date;
    }

    public String getSerial(){
        return serial;
    }

    public boolean getDeleted(){
        return deleted;
    }


    public void setId (long _id){
        id=_id;
    }

    public void setTypeId(long _type_id){
        type_id = _type_id;
    }

    public void setDescription(String _description){
        description = _description;
    }

    public void setByDate(Date _by_date){
        by_date = _by_date;
    }

    public void setWarrantyEndDate (Date _warranty_end_date){
        warranty_end_date = _warranty_end_date;
    }

    public void setSerial (String _serial){
        serial= _serial;
    }

    public void setDeleted (boolean _deleted){
        deleted=_deleted;
    }

    public ComponentTypeEntity getComponentType() {
        return componentType;
    }

    public void setComponentType(ComponentTypeEntity componentType) {
        this.componentType = componentType;
    }

    @Override
    public String toString(){
        return "ComponentEntity{" + "id=" + id +
                ", typeId=" + type_id +
                ", description=" + description +
                ", by_date=" + by_date +
                ", warranty_end_date=" + warranty_end_date +
                ", serial=" + serial +
                ", deleted=" + deleted + 
                ",componentType=" + componentType + '}';
    }
}
